﻿using BPMS.Business;
using BPMS.Entity;
using BPMS.IBusiness;
using DotNet.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace BPMS.MVC.Controllers
{
    public class OrganizeController : BaseController
    {
        BPMS_OrganizationIBLL bpms_organizationibll = new BPMS_OrganizationBLL();
        BPMS_Organization bpms_organization = new BPMS_Organization();
        BPMS_DataPermissionIBLL bpms_datapermissionibll = new BPMS_DataPermissionBLL();

        #region 组织机构列表
        /// <summary>
        /// 组织机构列表
        /// </summary>
        /// <returns></returns>
        public ActionResult OrganizeList()
        {
            return View();
        }

        #region 组织架构拼接table
        /// <summary>
        /// 组织架构表
        /// </summary>
        public string GetTreeTable()
        {
            StringBuilder TableTreeList = new StringBuilder();
            IList list = bpms_organizationibll.GetListSys();
            int eRowIndex = 0;
            foreach (BPMS_Organization entity in list)
            {
                if (entity.ParentId == "0")
                {
                    string trID = "node-" + eRowIndex.ToString();
                    TableTreeList.Append("<tr id='" + trID + "'>");
                    TableTreeList.Append("<td style='width: 230px;padding-left:20px;'><img src='/Themes/images/16/house.png' style='vertical-align: middle;' alt=''/><span style='padding-left:8px;'>" + entity.FullName + "</span></td>");
                    TableTreeList.Append("<td style='width: 130px;'>" + entity.Code + "</td>");
                    TableTreeList.Append("<td style='width: 60px;text-align:center;'>" + entity.Category + "</td>");
                    TableTreeList.Append("<td style='width: 120px;text-align:center;'>" + entity.ShortName + "</td>");
                    TableTreeList.Append("<td style='width: 100px;text-align:center;'>" + entity.Manager + "</td>");
                    TableTreeList.Append("<td style='width: 100px;text-align:center;'>" + entity.OuterPhone + "</td>");
                    TableTreeList.Append("<td style='width: 100px;text-align:center;'>" + entity.Manager + "</td>");
                    TableTreeList.Append("<td style='width: 60px;text-align:center;'>" + IsEnabled(entity.Enabled) + "</td>");
                    TableTreeList.Append("<td>" + entity.Description + "</td>");
                    TableTreeList.Append("<td style='display:none'>" + entity.OrganizationId + "</td>");
                    TableTreeList.Append("</tr>");
                    //创建子节点
                    TableTreeList.Append(GetTableTreeNode(entity.OrganizationId, list, trID));
                    eRowIndex++;
                }
            }
            return TableTreeList.ToString();
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTableTreeNode(string parentID, IList list, string parentTRID)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            int i = 1;
            foreach (BPMS_Organization entity in list)
            {
                if (entity.ParentId == parentID)
                {
                    string trID = parentTRID + "-" + i.ToString();
                    sb_TreeNode.Append("<tr id='" + trID + "' class='child-of-" + parentTRID + "'>");
                    sb_TreeNode.Append("<td style='padding-left:20px;'><img src='/Themes/images/16/" + CategoryToImg(entity.Category) + "' style='vertical-align: middle;' alt=''/><span style='padding-left:8px;'>" + entity.FullName + "</span></td>");
                    sb_TreeNode.Append("<td style='width: 130px;'>" + entity.Code + "</td>");
                    sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + entity.Category + "</td>");
                    sb_TreeNode.Append("<td style='width: 120px;text-align:center;'>" + entity.ShortName + "</td>");
                    sb_TreeNode.Append("<td style='width: 100px;text-align:center;'>" + entity.Manager + "</td>");
                    sb_TreeNode.Append("<td style='width: 100px;text-align:center;'>" + entity.OuterPhone + "</td>");
                    sb_TreeNode.Append("<td style='width: 100px;text-align:center;'>" + entity.Manager + "</td>");
                    sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + IsEnabled(entity.Enabled) + "</td>");
                    sb_TreeNode.Append("<td>" + entity.Description + "</td>");
                    sb_TreeNode.Append("<td style='display:none'>" + entity.OrganizationId + "</td>");
                    sb_TreeNode.Append("</tr>");
                    //创建子节点
                    sb_TreeNode.Append(GetTableTreeNode(entity.OrganizationId, list, trID));
                    i++;
                }
            }
            return sb_TreeNode.ToString();
        }
        /// <summary>
        /// 是否有效
        /// </summary>
        /// <param name="Enabled">是否有效</param>
        /// <returns></returns>
        public string IsEnabled(int? Enabled)
        {
            if (Enabled == 1)
            {
                return "<img src='/Themes/Images/checkmark.gif'/>";
            }
            else
            {
                return "<img src='/Themes/Images/checknomark.gif'/>";
            }
        }
        #endregion

        public void LoadAction()
        {
            string active = Request["action"];                                          //提交类型
            string key = Request["key"];                                                //主键
            switch (active)
            {
                case "OrgTreeList"://加载组织架构列表
                    Response.Write(GetTreeTable());
                    Response.End();
                    break;
                case "Delete":
                    if (bpms_organizationibll.IsBelowMenu(key))
                    {
                        bpms_organization = bpms_organizationibll.GetEntity(key);
                        Response.Write(string.Format(MessageHelper.MSG0010, bpms_organization.FullName));
                        Response.End();
                    }
                    else
                    {
                        Response.Write(bpms_organizationibll.Delete(key));
                        Response.End();
                    }
                    break;
                case "LoadBindDrop"://绑定下拉框，公司，部门，工作组
                    IList list = bpms_organizationibll.GetList();
                    Response.Write(JsonHelper.DropToJson<BPMS_Organization>(bpms_organizationibll.GetList(), "JSON"));
                    Response.End();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 组织机构表单
        /// <summary>
        /// 组织机构表单
        /// </summary>
        /// <returns></returns>
        public ActionResult OrganizeForm()
        {
            return View();
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="bpms_organization">组织机构</param>
        /// <returns></returns>
        public ActionResult AcceptClick(BPMS_Organization bpms_organization)
        {
            string key = Request["key"];                                                //主键
            bool IsOk = false;
            if (!string.IsNullOrEmpty(key))//判断是否编辑
            {
                bpms_organization.OrganizationId = key;
                bpms_organization.ModifyDate = DateTime.Now;
                bpms_organization.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                bpms_organization.ModifyUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_organizationibll.Update(bpms_organization);
            }
            else
            {
                bpms_organization.OrganizationId = CommonHelper.GetGuid;
                bpms_organization.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                bpms_organization.ModifyUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_organizationibll.Insert(bpms_organization);
                if (IsOk)
                {
                    bpms_datapermissionibll.AddItemDefault(SessionHelper.GetSessionUser().RoleId, "Organization", bpms_organization.OrganizationId, SessionHelper.GetSessionUser().UserId);
                }
            }
            return Content(IsOk.ToString().ToLower());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public ActionResult InitControl(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                bpms_organization = bpms_organizationibll.GetEntity(key);
                string strJson = JsonHelper.ObjectToJson(bpms_organization);

                //获取上级菜单信息
                bpms_organization = bpms_organizationibll.GetEntity(bpms_organization.ParentId);
                strJson = strJson.Insert(1, "\"ParentName\":\"" + bpms_organization.FullName + "\",");
                return Content(strJson);
            }
            return null;
        }
        /// <summary>
        /// 加载上级节点
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTreeList_Form()
        {
            IList list = bpms_organizationibll.GetList();
            StringBuilder strHtml = new StringBuilder();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == "0");
            strHtml.Append("<li>");
            strHtml.Append("<div id='0'><img src='/Themes/images/16/house.png' style='vertical-align: middle;' alt=''/><span>父节点</span></div>");
            strHtml.Append("</li>");
            foreach (BPMS_Organization entity in itemNode)
            {
                strHtml.Append("<li>");
                strHtml.Append("<div id='" + entity.OrganizationId + "'><img src='/Themes/images/16/house.png' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                //创建子节点
                strHtml.Append(GetTreeNode_Form(entity.OrganizationId, list));
                strHtml.Append("</li>");
            }
            return Content(strHtml.ToString());
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTreeNode_Form(string ParentId, IList list)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == ParentId);
            if (itemNode.Count > 0)
            {
                sb_TreeNode.Append("<ul>");
                foreach (BPMS_Organization entity in itemNode)
                {
                    sb_TreeNode.Append("<li>");
                    sb_TreeNode.Append("<div id='" + entity.OrganizationId + "'><img src='/Themes/images/16/" + CategoryToImg(entity.Category) + "' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                    //创建子节点
                    sb_TreeNode.Append(GetTreeNode_Form(entity.OrganizationId, list));
                    sb_TreeNode.Append("</li>");
                }
                sb_TreeNode.Append("</ul>");
            }
            return sb_TreeNode.ToString();
        }
        #endregion

        #region 组织架构图
        /// <summary>
        /// 组织架构图
        /// </summary>
        /// <returns></returns>
        public ActionResult OrganizeChart()
        {
            ViewData["TreeOrgCharthtml"] = GetTreeOrgChart();
            return View();
        }
        /// <summary>
        /// 组织架构图
        /// </summary>
        public string GetTreeOrgChart()
        {
            StringBuilder strHtml_OrgChart = new StringBuilder();
            IList list = bpms_organizationibll.GetListSys();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == "0");
            foreach (BPMS_Organization entity in itemNode)
            {
                string itemid = "v" + entity.OrganizationId.Replace("-", "");
                strHtml_OrgChart.Append("var " + itemid + " = new OrgNode();");
                strHtml_OrgChart.Append("" + itemid + ".Text = \"" + entity.FullName + "\";");
                strHtml_OrgChart.Append("" + itemid + ".Description = \"" + entity.ShortName + "\";");
                //strHtml_OrgChart.Append("" + itemid + ".Link = \"#\";");
                //创建子节点
                strHtml_OrgChart.Append(GetTreeNodeOrgChart(entity.OrganizationId, list));
            }
            return strHtml_OrgChart.ToString();
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTreeNodeOrgChart(string ParentId, IList list)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == ParentId);
            if (itemNode.Count > 0)
            {
                foreach (BPMS_Organization entity in itemNode)
                {
                    string itemid = "v" + entity.OrganizationId.Replace("-", "");
                    string itemParentId = "v" + ParentId.Replace("-", "");
                    sb_TreeNode.Append("var " + itemid + " = new OrgNode();");
                    sb_TreeNode.Append("" + itemid + ".Text = \"" + entity.FullName + "\";");
                    sb_TreeNode.Append("" + itemid + ".Description = \"" + entity.ShortName + "\";");
                    //sb_TreeNode.Append("" + itemid + ".Link = \"#\";");
                    sb_TreeNode.Append("" + itemParentId + ".Nodes.Add(" + itemid + ");");
                    //创建子节点
                    sb_TreeNode.Append(GetTreeNodeOrgChart(entity.OrganizationId, list));
                }
            }
            return sb_TreeNode.ToString();
        }
        #endregion

        #region 组织结构树
        /// <summary>
        /// 组织结构树
        /// </summary>
        /// <returns></returns>
        public ActionResult OrganizeTree()
        {
            return View();
        }
        /// <summary>
        /// 组织机构 拼接树
        /// </summary>
        public ActionResult GetTree()
        {
            StringBuilder strHtml = new StringBuilder();
            IList list = bpms_organizationibll.GetList2();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == "0");
            foreach (BPMS_Organization entity in itemNode)
            {
                strHtml.Append("<li>");
               
                strHtml.Append( "<div class='divTree' Category='" + entity.Category + "' id='" + entity.OrganizationId + "'><img src='/Themes/images/16/house.png' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                //创建子节点
                strHtml.Append(GetTreeNode(entity.OrganizationId, list));
                strHtml.Append("</li>");
            }
            return Content(strHtml.ToString());
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTreeNode(string ParentId, IList list)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            List<BPMS_Organization> itemNode = IListHelper.IListToList<BPMS_Organization>(list).FindAll(t => t.ParentId == ParentId);
            if (itemNode.Count > 0)
            {
                sb_TreeNode.Append("<ul>");
                foreach (BPMS_Organization entity in itemNode)
                {

                    sb_TreeNode.Append("<li title='" + entity.Category + "'>");
                    sb_TreeNode.Append("<div class='divTree' Category='" + entity.Category + "' id='" + entity.OrganizationId + "'><img src='/Themes/images/16/" + CategoryToImg(entity.Category) + "' style='vertical-align: middle;' alt=''/><span>" + entity.FullName + "</span></div>");
                    //创建子节点
                    sb_TreeNode.Append(GetTreeNode(entity.OrganizationId, list));
                    sb_TreeNode.Append("</li>");
                }
                sb_TreeNode.Append("</ul>");
            }
            return sb_TreeNode.ToString();
        }
        public string CategoryToImg(string Category)
        {
            string img = "";
            if (Category == "区域")
            {
                img = "house_star.png";
            }
            if (Category == "公司")
            {
                img = "chart_organisation.png";
            }
            if (Category == "子公司")
            {
                img = "flag_green.png";
            }
            if (Category == "部门")
            {
                img = "wand.png";
            }
            if (Category == "工作组")
            {
                img = "users.png";
            }
            return img;
        }
        #endregion
    }
}
